﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace 门诊医生工作站
{
    public partial class frm_ModiPassword : Form
    {
        public frm_ModiPassword()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void btn_Modi_Click(object sender, EventArgs e)
        {
           
            if (this.txb_OriPassword.Text.Trim() == "")
            {
                MessageBox.Show("原密码不能为空！");
                this.txb_OriPassword.Focus();
                return;
            }
            if (this.txb_NewPassword.Text.Trim() == "")
            {
                MessageBox.Show("新密码不能为空！");
                this.txb_NewPassword.Focus();
                return;
            }
            if (this.txb_CheckPassword.Text.Trim() == "")
            {
                MessageBox.Show("请确认新密码！");
                this.txb_CheckPassword.Focus();
                return;
            }
            if (this.txb_NewPassword.Text!=this.txb_CheckPassword.Text)
            {
                MessageBox.Show("两次密码不一致！");
                this.txb_CheckPassword.Focus();
                return;
            }
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
            //SqlCommand sqlCommand1 = sqlConnection.CreateCommand();     //调用sqlConnection的方法CreateCommand创建sqlCommand并建立连接       
            //sqlCommand1.CommandText = $"SELECT COUNT(1) FROM dbo.tb_User WHERE Password=HASHBYTES('MD5',@Password)";
            //sqlCommand1.Parameters.AddWithValue("@Password",this.txb_OriPassword.Text);
           
            //int rowCount1 = (int)sqlCommand1.ExecuteScalar();
            //if (rowCount1==1)
            //{
                SqlCommand sqlCommand2 = sqlConnection.CreateCommand();
                sqlCommand2.CommandText = $"UPDATE dbo.tb_User "
                                        + $" SET"
                                        + $" Password=HASHBYTES('MD5','{this.txb_NewPassword.Text.Trim()}')"                                           
                                        + $" WHERE Password=HASHBYTES('MD5','{this.txb_OriPassword.Text.Trim()}')";
                sqlConnection.Open();
              int rowAffected = sqlCommand2.ExecuteNonQuery();
              sqlConnection.Close();
                if (rowAffected !=0)
                {
                    MessageBox.Show("修改密码成功！请重新登录！");
                this.DialogResult = DialogResult.OK;
                this.Close();
                frm_Login frm_Login = new frm_Login();
                frm_Login.Show();

                }
            //}
           


        }
    }
}
